package com.yss.sofa.report.engine.util;

import org.apache.commons.lang3.StringUtils;


public final class NumberUtil {
	public static String to26(int n) throws Exception{
		if(n<=0){
			throw new Exception("无法转换十进制数["+n+"]为二十六进制数");
		}
		String s = "";
	    while (n > 0){
	        int m = n % 26;
	        if (m == 0) m = 26;
	        s = (char)(m + 64) + s;
	        n = (n - m) / 26;
	    }
	    return s;
	} 
	
	public static int to10(String s) throws Exception{
	    if (StringUtils.isEmpty(s)){
	    	throw new Exception("无效的二十六进制数["+s+"]");
	    }
	    s=s.toUpperCase();
	    int n = 0;
	    for (int i = s.length() - 1, j = 1; i >= 0; i--, j *= 26){
	        char c = s.charAt(i);
	        if (c < 'A' || c > 'Z'){
	        	throw new Exception("无效的二十六进制数["+s+"]");
	        }
	        n += ((int)c - 64) * j;
	    }
	    return n;
	}
	
	public static String toExcelPoint(int row, int col) throws Exception{
		return to26(col+1)+(row+1);
	}
}
